/**
 * 
 */
package com.wgo.precise.client.ui.view.util.celleditors;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
import org.eclipse.ui.dialogs.SelectionDialog;

import com.wgo.precise.client.ui.controller.ModelStatus;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.NodeChild;
import com.wgo.precise.client.ui.model.ProjectWrapper;
import com.wgo.precise.client.ui.model.facade.INodeChild;
import com.wgo.precise.client.ui.model.facade.INodeParent;
import com.wgo.precise.client.ui.model.facade.ModelSystem;
import com.wgo.precise.client.ui.view.editor.properties.DetailsSheetViewer;
import com.wgo.precise.client.ui.view.util.ExtendedViewerSorter;
import com.wgo.precise.client.ui.view.util.LinkListPropertySource;
import com.wgo.precise.client.ui.view.util.ModelItemLabelProvider;
import com.wgo.precise.client.ui.view.util.contentproviders.ModelTreeViewContentProvider;
import com.wgo.precise.client.ui.view.util.validators.RequirementStatusSelectionValidator;
import com.wgo.precise.client.ui.view.util.viewfilters.RequirementViewFilter;


/**
 * @author petterei
 *
 * @version $Id: RequirementsCellEditor.java,v 1.1 2006-03-07 14:56:23 petterei Exp $
 */

public class RequirementsCellEditor extends DialogCellEditor {
	private ClientModelItem clientModelItem;
	public RequirementsCellEditor(Composite parent, int style) {
		super(parent, style);
		if (parent instanceof DetailsSheetViewer.PropertySheetTree) {
			clientModelItem = ((DetailsSheetViewer.PropertySheetTree)parent).getClientModelItem();
		}
	}
	public RequirementsCellEditor(Composite parent) {
		this(parent, SWT.NONE);
	}
	@Override
	protected Object openDialogBox(Control cellEditorWindow) {
		CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(cellEditorWindow.getShell(), new ModelItemLabelProvider(), new ModelTreeViewContentProvider()) ;
		dialog.setTitle("Select Requiremnts");
		dialog.setSorter(new ExtendedViewerSorter());
		dialog.setValidator(new RequirementStatusSelectionValidator(clientModelItem));
		dialog.setInput(RequirementPlugin.getInstance().getSession().getWrapperFactory().getSystem(ProjectWrapper.class));
		dialog.setContainerMode(false);
		dialog.setEmptyListMessage("No requirements available.");
		if (null != getValue() && getValue() instanceof LinkListPropertySource) {
			List<IAdaptable> currentLinkedRequirements = ((LinkListPropertySource)getValue()).getItems();
			Set<INodeParent> expanded = new HashSet<INodeParent>();
			for (IAdaptable adaptable : currentLinkedRequirements){
				if (adaptable instanceof NodeChild) {
					INodeChild child = (INodeChild) adaptable;
					getParents(child, expanded);
				}
			}
			dialog.setExpandedElements(expanded.toArray());
			dialog.setInitialElementSelections(currentLinkedRequirements);
		} else if (getValue() != null) {
			RequirementPlugin.log(new ModelStatus(IStatus.WARNING, getClass() + " openDialogBox(): CellEditorValue had value of invalid type set."));
		}
		dialog.addFilter(new RequirementViewFilter());
		LinkListPropertySource values = new LinkListPropertySource(new ArrayList<IAdaptable>());
		if (dialog.open() == SelectionDialog.OK && dialog.getResult().length > 0){
			Object[] result = dialog.getResult();
			if (0 < result.length) {
				List<IAdaptable> selected = new ArrayList<IAdaptable>();
				for (Object object : result) {
					if (object instanceof IAdaptable) {
						IAdaptable adaptable = (IAdaptable) object;
						selected.add(adaptable);
					}
				}
				values.getItems().addAll(selected);
			}
		}
		return values;
	}
	@SuppressWarnings("unchecked")
	private void getParents(INodeChild child, Set<INodeParent> parents) {
		INodeParent parent = child.getParent();
		if (! (parent instanceof ModelSystem)) {
			parents.add(parent);
			getParents(parent, parents);
		}
	}
}
